<?php
abstract class ArrowAbstractTableFilter extends ArrowTagContainer{
	
	protected static $registry = array();
	
	protected $table;
	protected $tableId;
	protected $field;
	
	public function configure(){
		$this->addProperties(array(
			"type" => false,
			"strict" => false,
			"active" => 0,
			"field" => false,
			"width" => "95%",
			"process" => true
		));
		//$this->setContentCodeKept(true);
		$this->setDisplay(false);
	}
	
	public function init(){
		$this->table = $this->getParent("ArrowTable");
		$this->tableId = $this->table->getId();
		if( !isset(self::$registry[$this->tableId]) )
			self::$registry[$this->tableId] = array();
		$column = $this->getParent("ArrowAbstractTableColumn");
		
		$field = $this->getProperty("field");
		if($field)
			$this->field = $field;
		else
			$this->field = $column->getProperty("field");
		
		$registry = $this->table->getProperty("filtersRegistry");
		if(!empty($registry))
			self::$registry[$this->tableId] = (array) json_decode(base64_decode($registry), true);
		
	}
	
	protected function getField(){
		return $this->field;
	}
	
	protected function getFilterLink($value){
		$registry = self::$registry[$this->tableId];
		$registry[$this->field] = $value;
		return ArrowCtrlStateManager::getDefault()->getStateChangeLink($this->table, array( "filtersRegistry" => base64_encode(json_encode($registry)) ));
	}
	
	protected function getResetLink(){
		$registry = self::$registry[$this->tableId];
		unset($registry[$this->field]);
		return ArrowCtrlStateManager::getDefault()->getStateChangeLink($this->table, array( "filtersRegistry" => base64_encode(json_encode($registry)) ));
	}
	
	protected function getResetRegistry(){
		$registry = self::$registry[$this->tableId];
		unset($registry[$this->field]);
		return base64_encode(json_encode($registry));
	} 
	
	public function generate(){
		$val = isset(self::$registry[$this->tableId][$this->field])?self::$registry[$this->tableId][$this->field]:false;
		$val = htmlspecialchars($val['value']);
		
		$str = '<a href="#" class="ctrl-table-filter-show"></a>';

		$str.= '<div style="display: none;" >';
		$str.= '<div class="ctrl-table-filter ctrl-table-filter-'.$this->getProperty("type").'" style="float: left; width: '.$this->getProperty("width").'px;">';
		$str.= $this->generateFilter($val);
		$str.= '<input type="hidden" name="field" value="'.$this->field.'" />';
		
		if($this->getProperty('strict') !== false)
			$str.= '<input type="hidden" name="strict" value="'.$this->field.'" />';
		
		if($this->getProperty('process') == true )
			$str.= '<input type="hidden" name="process" value="'.$this->field.'" />';
		
		$str.= '</div>';
		$str.= '</div>';
		return $str;
	}
	
	public function generateSetFilter($fieldName) {
		
		$val = isset(self::$registry[$this->tableId][$this->field])?self::$registry[$this->tableId][$this->field]:false;
		$val = htmlspecialchars($val['value']);
		
		$str = '';
		if($val != '' && $val != '_arrow_range_' && $val != '_arrow_sep_') {
			$generate_value = $this->generateSetFilterValues($val) ;
			if(!empty( $generate_value ) ) {
				$str.= '<div class="ctrl-table-set-filter" >';
				$str.= '<div>'.$fieldName.':</div>'.$generate_value;
				$str.= '</div>';
			}
		}
		
		return $str;
	}
	
	public abstract function generateSetFilterValues($values);
	
	public abstract function generateFilter($value);
	
	public function generateBlockStart(){
		return "";
	}
	
	public function generateBlockEnd(){
		return "";
	}
	
	public function generateOutputCode(){
		return "";
		
	}
	
}
?>
